﻿using System;
using System.Text;
using System.Windows.Forms;
using SharePointUtilitiesForDevelopers.Action;
using SharePointUtilitiesForDevelopers.Actions.Forms;

namespace SharePointUtilitiesForDevelopers.Actions
{
    /// <summary>
    /// Generates a sql statement to select the SharePoint version from the config database.
    /// </summary>
    public class GenerateFarmVersionSqlAction: IAction
    {
        /// <summary>
        /// Gets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return Properties.Resources.GenerateFarmSqlVersionActionTitle; }
        }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { return Properties.Resources.GenerateFarmSqlVersionActionDescription; }
        }

        /// <summary>
        /// Gets the target.
        /// </summary>
        /// <value>The target.</value>
        public ActionTarget Target
        {
            get { return ActionTarget.All; }
        }

        /// <summary>
        /// Executes the action.
        /// </summary>
        /// <param name="actionContext">The context.</param>
        public void Execute(ActionContext actionContext)
        {
            // Show form
            var form = ServiceLocator.Resolve<SqlForm>();
            form.Sql = GenerateSelectStatement();
            form.ShowDialog(actionContext.Owner as IWin32Window);
        }

        /// <summary>
        /// Generates a SharePoint version select statement using the default config db name.
        /// </summary>
        /// <returns>A sql select statement.</returns>
        private static string GenerateSelectStatement()
        {
            return GenerateSelectStatement("SharePoint_Config");
        }

        /// <summary>
        /// Generates a SharePoint version select statement using the specified config db name.
        /// </summary>
        /// <returns>A sql select statement.</returns>
        private static string GenerateSelectStatement(string configDbName)
        {
            // Create sql
            var sql = new StringBuilder();
            sql.AppendLine("SELECT");
            sql.AppendLine("\t[VersionId]");
            sql.AppendLine("\t,[Version]");
            sql.AppendLine("\t,[Id]");
            sql.AppendLine("\t,[UserName]");
            sql.AppendLine("\t,[TimeStamp]");
            sql.AppendLine("\t,[FinalizeTimeStamp]");
            sql.AppendLine("\t,[Mode]");
            sql.AppendLine("\t,[ModeStack]");
            sql.AppendLine("\t,[Updates]");
            sql.AppendLine("\t,[Notes]");
            sql.AppendLine("FROM");
            sql.AppendFormat("\t[{0}].[dbo].[Versions]{1}", configDbName, Environment.NewLine);
            sql.AppendLine("WHERE");
            sql.AppendLine("\tVersionId = '00000000-0000-0000-0000-000000000000'");
            sql.AppendLine("ORDER BY Id DESC");

            return sql.ToString();
        }

    }
}